<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('设置靓号')" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-agentInfo-insertCodeBody">
        <div class="form-group">
            <label class="col-sm-3 control-label">代理编号：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" readonly="true" name="agentNum" th:value="${agentInfo?.agentNum}"/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">代理商名称：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" readonly="true" name="agentName" th:value="${agentInfo?.agentName}"/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">靓号：</label>
            <div class="col-sm-8" >
                <input class="form-control"  onblur="valiCode(this)" type="text" minlength="9" maxlength="9" th:value="${agentInfo?.beautiful}" onkeyup = "value=value.replace(/[^\d]/g,'')" name="beautiful" />
                <div id="msg"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">靓号名称：</label>
            <div class="col-sm-8" >
                <input class="form-control"  onblur="valiCodeName(this)" type="text" minlength="3" maxlength="60" th:value="${agentInfo?.beautifulName}"  name="beautifulName" />
                <div id="msgName"></div>
            </div>
        </div>
        <!--<div class="form-group" th:if="${agentInfo.beautiful == null || agentInfo.beautiful == ''}">
            <label class="col-sm-3 control-label">填写靓号：</label>
            <div class="col-sm-8" >
                <input class="form-control"  onblur="valiCode(this)" type="text" minlength="9" maxlength="9"  onkeyup = "value=value.replace(/[^\d]/g,'')" name="beautiful" />
                <div id="msg"></div>
            </div>
        </div>-->
    </form>
</div>
<th:block th:include="include :: footer" />
<script type="text/javascript">
    var prefix = ctx + "agentInfo/agentInfo";
    var flag = false;
    $("#form-agentInfo-resetPwd").validate({
        rules:{
            password:{
                required:true,
                minlength: 5,
                maxlength: 20
            },
        },
        focusCleanup: true
    });

    function msg(message) {
        if ($.common.isNotEmpty(message)){
            var html = '<span style="color: #8a1f11" class="help-block m-b-none">' + message + '<i class="fa fa-info-circle"></i></span>';
            $("#msg").html(html);
        } else {
            $("#msg").html("");
        }
    }
    function msgName(message) {
        if ($.common.isNotEmpty(message)){
            var html = '<span style="color: #8a1f11" class="help-block m-b-none">' + message + '<i class="fa fa-info-circle"></i></span>';
            $("#msgName").html(html);
        } else {
            $("#msgName").html("");
        }
    }

    /**
     * 验证靓号是否可用
     */
    function valiCode(obj) {
        var url = prefix + "/valiCode";
        //代理编号
        var agentNum = $("input[name='agentNum']").val();
        var beautiful = obj.value;
        if ($.common.isEmpty(beautiful)){
            msg("请填写靓号");
            flag = false;
            $(obj).css('border-color', 'red');
            return;
        }
        if (beautiful.length != '9'){
            msg("请输入9位数靓号");
            flag = false;
            $(obj).css('border-color', 'red');
            return;
        }
        $.post(url,{'agentNum':agentNum,'beautiful':beautiful},function(result){
            if (result.code == '0'){
                flag = true;
                msg("");
            }else {
                msg(result.msg);
                flag = false;
                return;
            }
        });
    }

    /**
     * 验证靓号名称是否合法
     */
    function valiCodeName(obj) {
        var url = prefix + "/valiCodeName";
        //代理编号
        var agentNum = $("input[name='agentNum']").val();
        var beautifulName = obj.value;
        if ($.common.isEmpty(beautifulName)){
            msgName("请填写靓号");
            flag = false;
            return;
        }
        $.post(url,{'agentNum':agentNum,'beautifulName':beautifulName},function(result){
            if (result.code == '0'){
                flag = true;
                msgName("");
            }else {
                msgName(result.msg);
                flag = false;
                return;
            }
        });
    }



    function submitHandler() {
        if ($.validate.form()) {
            if (flag){
                $.operate.save(prefix + "/updateBeautiful", $('#form-agentInfo-insertCodeBody').serialize());
            }
        }
    }
</script>
</body>

</html>
